package com.itheima.easy.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.itheima.easy.constant.TradeConstant;
import com.itheima.easy.handler.PagePayHandler;
import com.itheima.easy.vo.TradeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName PagePayController.java
 * @Description PC网页支付
 */
@RequestMapping("trade-page-feign")
@RestController
@Api(tags = "PC网页")
public class PagePayController {

    @Autowired
    PagePayHandler aliPagePayHandler;

    @Autowired
    PagePayHandler wechatPagePayHandler;

    /***
     * @description 生成交易表单，渲染后自动跳转支付宝或者微信引导用户完成支付
     * @param tradeVo 订单单
     * @return TradeVo对象中的placeOrderJson：阿里云form表单字符串、微信prepay_id标识
     */
    @PostMapping("trade-page/page-trade")
    @ApiOperation(value = "PC网页支付",notes = "PC网页支付")
    @ApiImplicitParam(name = "tradeVo",value = "交易单",required = true,dataType = "TradeVo")
    @ApiOperationSupport(includeParameters ={"tradeVo.tradeOrderNo","tradeVo.returnUrl",
            "tradeVo.tradeAmount","tradeVo.tradeChannel","tradeVo.openId","tradeVo.memo"})
    TradeVo pageTrade(@RequestBody TradeVo tradeVo){
        TradeVo tradeVoResult = null;
        if (TradeConstant.TRADING_CHANNEL_ALI_PAY.equals(tradeVo.getTradeChannel())){
            tradeVoResult= aliPagePayHandler.pageTrade(tradeVo);
        }else if (TradeConstant.TRADING_CHANNEL_WECHAT_PAY.equals(tradeVo.getTradeChannel())){
            tradeVoResult= wechatPagePayHandler.pageTrade(tradeVo);
        }else {
            throw new RuntimeException("请输入渠道！");
        }
        return tradeVoResult;
    }
}
